package mis510.homefinders;

import java.util.StringTokenizer;

import mis510.homefinders.util.SystemUiHider;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 * @see SystemUiHider   

3576 Madison Ave, San Diego, CA
 */
public class SearchProperty extends Activity implements LocationListener{
    
    Button search_button;
    Button rm_button;
    Button curloc_button;
    EditText search_address;
    Intent nextActivity;
    private LocationManager lManager;
    Location lastKnownLocation;
    Toast toast_info;
    //SQLiteDatabase db = SQLiteDatabase.openDatabase("/Users/Annie/Documents/workspace/HomeFinders", null, 0);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        
        search_button = (Button) findViewById(R.id.search_button);
        rm_button = (Button) findViewById(R.id.rm_button);
        curloc_button = (Button) findViewById(R.id.curloc_button);
        search_address = (EditText) findViewById(R.id.search_address);
        
        lManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
            lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    0, 0, this);
        else
            Log.i("Test", "network provider unavailable");

        lastKnownLocation =
                lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (lastKnownLocation != null) {
            Log.i("Test", lastKnownLocation.getLatitude() + ", "
                    + lastKnownLocation.getLongitude());
            lManager.removeUpdates(this);
        } else
            Log.i("Test", "null");
    
   

        // Set up the user interaction to manually show or hide the system UI.
        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	String address = search_address.getText().toString();
            	System.out.println("address: "+address);
            	StringTokenizer st = new StringTokenizer(address, ",");
            	if (String.valueOf(address).trim().length()>0 && st.countTokens() == 3)
            	{
            		nextActivity = new Intent(SearchProperty.this, Evaluation.class);
            		//startActivity(nextActivity);
            		nextActivity.putExtra("address", address);
            		startActivityForResult(nextActivity, 1);
            	}
            	else 
            	{
            		toast_info = Toast.makeText(getApplicationContext() , "Please provide an address in correct format, e.g. stree, city, state", Toast.LENGTH_SHORT);
            		toast_info.show();
            	}
            }
        });
        
        rm_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	nextActivity = new Intent(SearchProperty.this, Rm_login_req.class);
            	startActivity(nextActivity);
            }
        });
        
        curloc_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//            	nextActivity = new Intent(SearchProperty.this, Rm_login_req.class);
//            	startActivity(nextActivity);
            	if(lastKnownLocation != null)
            	{
            		toast_info = Toast.makeText(getApplicationContext() , lastKnownLocation.getLatitude() + ", "+ lastKnownLocation.getLongitude(), Toast.LENGTH_SHORT);
            		toast_info.show();
            	}
            	else
            	{
            		toast_info = Toast.makeText(getApplicationContext() , "Location unavailable", Toast.LENGTH_SHORT);
            		toast_info.show();
            	}
            }
        });

    }

    @Override
    public void onLocationChanged(Location location) {
        Log.i("Test", "location changed: " + location.getLatitude() + ", "
                + location.getLongitude());
    }

	
	@Override
	public void onProviderDisabled(String Provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String Provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String Provider, int status, Bundle extra) {
		// TODO Auto-generated method stub
		
	}
}
